package com.doctor.ui.homedoctor;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.doctor.adapter.PatientFileAdapter;
import com.doctor.adapter.PatientFileAdapter.HomeDoctorAdapterCallback;
import com.doctor.bean.PatientFileBean;
import com.doctor.bean.PatientFileListBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.FileHelper;
import com.doctor.comm.FragmentHelper;
import com.doctor.comm.DialogHelper.DialogSelectCallback;
import com.doctor.comm.DialogHelper.NoticeDialogCallback;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;

public class SearchPatientFrag extends BaseFragment{

	/**
	 * 患者档案搜索
	 */
	
	private Context context;
	private String[] operation={"修改","删除","取消"};
	private PatientFileAdapter adapter;
	private ListView listView;
	private LinearLayout addBtn;    //添加按钮
	private AddPatientFileFrag addPatientFileFrag;
	private ModifyPatientFileFrag modifyPatientFileFrag;
	private LookPatientFileFrag lookPatientFileFrag;
	private List<PatientFileBean> beanList;
	private TextView searchCount;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context=getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.search_patient, container, false);
		initiaView(view);
		
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		addBtn=(LinearLayout) view.findViewById(R.id.addBtn);
		addBtn.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				//切换至增加患者档案frag
				if (addPatientFileFrag==null) {
					addPatientFileFrag=new AddPatientFileFrag();
				}
				FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
				FragmentHelper.setFragSwitchAnimation(transaction);
				transaction.replace(R.id.frag_container, addPatientFileFrag).addToBackStack(null).commit();
			}
		});
		searchCount=(TextView) view.findViewById(R.id.searchCount);
		beanList=((PatientFileListBean) getArguments().getSerializable(ConstConfig.BEAN)).getDataList();
		searchCount.setText("患者档案  共有"+beanList.size()+"条搜索结果：");
		
		listView=(ListView) view.findViewById(R.id.listView);
		adapter=new PatientFileAdapter(context,beanList,new HomeDoctorAdapterCallback() {			
			@Override
			public void onCallPhone(String phoneNumber) {
				// TODO Auto-generated method stub
				FileHelper.dialPhone(getActivity(), context, phoneNumber);
			}
		});
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				final PatientFileBean bean=(PatientFileBean) arg1.getTag(R.id.bean_tag);
				lookPatientFileFrag=null;
				lookPatientFileFrag=new LookPatientFileFrag();
				Bundle bundle=new Bundle();
				bundle.putSerializable(ConstConfig.BEAN, bean);
				lookPatientFileFrag.setArguments(bundle);
				FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
				FragmentHelper.setFragSwitchAnimation(transaction);
				transaction.replace(R.id.frag_container, lookPatientFileFrag).addToBackStack(null).commit();
				
			}
		});
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub				
				final PatientFileBean bean= (PatientFileBean) arg1.getTag(R.id.bean_tag);
				final int beanPosition=arg2;
				
				DialogHelper.selectDialog(context, null, operation, new DialogSelectCallback() {
					@Override
					public void onDialogSelected(int position1) {
						// TODO Auto-generated method stub
						if (position1==0) {
							//修改
							modifyPatientFileFrag=null;
							modifyPatientFileFrag=new ModifyPatientFileFrag();
							Bundle bundle=new Bundle();
							bundle.putSerializable(ConstConfig.BEAN, bean);
							modifyPatientFileFrag.setArguments(bundle);
							FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
							FragmentHelper.setFragSwitchAnimation(transaction);
							transaction.replace(R.id.frag_container, modifyPatientFileFrag).addToBackStack(null).commit();
							
						} else if (position1==1) {
							//删除前确定是否有相关病历记录
							int count=DbOperator.getInstance(context).selectDiseaseCount(bean.getId());
							if (count>0) {
								DialogHelper.showConfirmDialog(context, bean.getName()+"有"+count+"条病历资料，请先删除相关的病历记录！");
								return;
							}
							
							DialogHelper.noticeDialog(context, null, new NoticeDialogCallback() {								
								@Override
								public void onClicked(int position2) {
									// TODO Auto-generated method stub
									if (position2==1) {
										//删除
										int row=DbOperator.getInstance(context).deleteData(ConstConfig.PATIENT_FILE_TABLE, bean.getId());
										String deleteResult=row>0?"成功！":"失败！";
										Toast.makeText(context, "删除"+deleteResult, Toast.LENGTH_SHORT).show();
										if (row>0) {
											//更新adapter数据
											adapter.removeBean(beanPosition);
										}
									}
								}
							});
						}
					}
				});
				return true;
			}
		});			
	}
	
}
